package com.bjt.wms.sys.controller;

import com.bjt.common.config.paragetter.BasePageRequest;
import com.bjt.wms.sys.model.DeliverySheet;
import com.bjt.wms.sys.model.FlowDetail;
import com.bjt.wms.sys.service.DeliverySheetService;
import com.bjt.wms.sys.service.FLowDetailService;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FlowDetailController extends Controller {

    @Inject
    private FLowDetailService fLowDetailService;

    @Inject
    private DeliverySheetService deliverySheetService;

    /**
     * 分页查询入库信息列表
     */
    public void queryInboundList(BasePageRequest<FlowDetail> basePageRequest) {
        Kv cond = Kv.create();
        if (StrKit.notBlank(getPara("sheet_code"))) {
            DeliverySheet deliverySheet=deliverySheetService.findByCode(getPara("sheet_code"));
            cond.set("sheet_id", deliverySheet.getId());
        }
        if (StrKit.notBlank(getPara("warehouse_id"))) {
            cond.set("warehouse_id", getParaToInt("warehouse_id"));
        }
        if (StrKit.notBlank(getPara("material_id"))) {
            cond.set("material_id", getParaToInt("material_id"));
        }

        if (StrKit.notBlank(getPara("dateRange"))) {
            cond.set("date_start", getPara("dateRange").split(" - ")[0]);
            cond.set("date_end", getPara("dateRange").split(" - ")[1]);
        }

        renderJson(fLowDetailService.queryInboundList(basePageRequest, cond));
    }

    /**
     * 分页查询出库信息列表
     */
    public void queryOutboundList(BasePageRequest<FlowDetail> basePageRequest) {
        Kv cond = Kv.create();
        if (StrKit.notBlank(getPara("warehouse_id"))) {
            cond.set("warehouse_id", getParaToInt("warehouse_id"));
        }
        if (StrKit.notBlank(getPara("material_id"))) {
            cond.set("material_id", getParaToInt("material_id"));
        }
        if (StrKit.notBlank(getPara("dateRange"))) {
            cond.set("date_start", getPara("dateRange").split(" - ")[0]);
            cond.set("date_end", getPara("dateRange").split(" - ")[1]);
        }
        renderJson(fLowDetailService.queryOutboundList(basePageRequest, cond));
    }

    public void image() {
        renderFile(new File(get("filePath")));
    }
}
